package com.lanou.sinasports.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.lanou.sinasports.R;
import com.lanou.sinasports.beans.NewsHot;
import com.lanou.sinasports.utils.BitmapCache;
import com.lanou.sinasports.utils.DoubleCache;

import java.util.List;

/**
 * Created by dllo on 16/1/13.
 */
public class NewsHotAdapter extends BaseAdapter {
    private Context context;
    private List<NewsHot> data;
    private ImageLoader imageLoader;
    private RequestQueue queue;

    public NewsHotAdapter(Context context, List<NewsHot> data) {
        this.context = context;
        this.data = data;
        queue= Volley.newRequestQueue(context);
        imageLoader=new ImageLoader(queue,new DoubleCache());
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder myViewHolder;
        if(convertView==null){
            convertView= LayoutInflater.from(context).inflate(R.layout.item_newshot,parent,false);
            myViewHolder=new MyViewHolder(convertView);
            convertView.setTag(myViewHolder);
        }else {
            myViewHolder= (MyViewHolder) convertView.getTag();

        }
        Log.d("NewsHotAdapter", data.get(position).getJktitle()+"");
            myViewHolder.tvNewsHotTitle.setText(data.get(position).getJktitle());
            myViewHolder.tvNewsHotSummary.setText(data.get(position).getJksummary());


                myViewHolder.imageView.setDefaultImageResId(R.mipmap.ic_main_tab_match_p);
                myViewHolder.imageView.setErrorImageResId(R.mipmap.ic_main_tab_team_p);
                myViewHolder.imageView.setImageUrl(data.get(position).getImage(), imageLoader);


        return convertView;
    }

    class MyViewHolder {

        NetworkImageView imageView;
        TextView  tvNewsHotTitle;
        TextView  tvNewsHotSummary;
        public MyViewHolder(View view) {
            imageView= (NetworkImageView) view.findViewById(R.id.iv_item_newshot);
            tvNewsHotTitle= (TextView) view.findViewById(R.id.tv_item_newshot_title);
            tvNewsHotSummary= (TextView) view.findViewById(R.id.tv_item_newshot_summary);
        }
    }
}
